package cn.itcast.gate.filters;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import reactor.core.publisher.Mono;

@Slf4j
@Configuration
public class FilterConfiguration {

    @Bean
    @Order(-2) // 值越小优先级越高
    public GlobalFilter globalFilter1(){

        return (((exchange, chain) -> {

            log.info("过滤器1的pre阶段！");

            return chain.filter(exchange).then(Mono.fromRunnable(() ->{

                log.info("过滤器1的post阶段！");

            }));

        }));

    }

    @Bean
    @Order(-1)
    public GlobalFilter globalFilter2(){

        return (((exchange, chain) -> {

            log.info("过滤器2的pre阶段！");

            return chain.filter(exchange).then(Mono.fromRunnable(() ->{

                log.info("过滤器2的post阶段！");

            }));

        }));

    }

    @Bean
    @Order(0)
    public GlobalFilter globalFilter3(){

        return (((exchange, chain) -> {

            log.info("过滤器3的pre阶段！");

            return chain.filter(exchange).then(Mono.fromRunnable(() ->{

                log.info("过滤器3的post阶段！");

            }));

        }));

    }

}
